<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExpoter;
use App\Admin\Extensions\Squad_monthExpoter;
use App\Models\Class_section_group;
use App\Models\Class_type;
use App\Models\Credits_squad;
use App\Models\Squad;
use App\Models\Squad_month;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\Student_lost;
use App\Models\Lost_detail;

use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Credits;
use Encore\Admin\Widgets\Table;
use App\Models\Campus;
use App\Models\Apply;
use App\Models\Term;
use Illuminate\Support\Facades\Input;

class Student_lostController extends Controller
{
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('学员流失');
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public function grid()
    {
        $grid = new Grid(new Student_lost());
        $current_term = Term::where('status','0')->first();
        $week = ceil((time()-strtotime($current_term->start_date))/3600/24/7);
        if (empty(Input::get())) {
            $grid->model()->where(['term_year'=>$current_term->year,'term_season'=>$current_term->season,'week'=>$week]);
        }
        

        $grid->paginate(20);

        $grid->class_teacher('班主任')->display(function($class_teacher){
            return DB::table('admin_users')->where('id',$this->class_teacher)->value('en_name');
        })->sortable();

        $grid->squad_name('班级')->sortable();

        $grid->squad_section('班段')->display(function($squad_section){
            if ($this->squad_section==0) {
                return 'N/A';
            }else{
                return Class_section_group::find($this->squad_section)->name;
            }
        });
        $grid->squad_group('分组')->display(function($squad_group){
            if ($this->squad_group==0) {
                return 'N/A';
            }else{
                return Class_section_group::find($this->squad_group)->name;
            }
        });
        $grid->squad_batch('开班批次')->sortable();
        $grid->column('流失时间')->display(function($term_season){
            if ($this->term_season=='1') {
                return "【".$this->term_year."年】【春季学期】【第".$this->week."周】";
            }elseif ($this->term_season=='2') {
                return "【".$this->term_year."年】【秋季学期】【第".$this->week."周】";
            }
        });

        // $grid->column('流失前人数')->display(function(){
        //     $stu_nums = Squad_month::where(['squad_id'=>$this->squad_id,'year'=>$this->term_year,'season'=>$this->term_season,'week'=>$this->week])->get(['stu_num'])->toArray();
        //         if (count($stu_nums)=='0') {
        //             return "N/A";
        //         }
        //         else{
        //             return $stu_nums[0]['stu_num'];
        //         }
        // });
        $grid->column('流失人数')->display(function(){
            return "<a class='btn btn-danger' href='student_lost_detail?squad_id=".$this->squad_id."&term_year=".$this->term_year."&term_season=".$this->term_season."&week=".$this->week."'>$this->lost_students</a>";
        });
        $grid->column('学习进度')->display(function(){
            return Student_lost::where(['squad_id'=>$this->squad_id,'term_year'=>$this->term_year,'term_season'=>$this->term_season,'week'=>$this->week])->value('study_jindu');
        });
        $grid->column('流失原因')->display(function(){
            $memos = Apply::where(['status'=>'1','squad_id'=>$this->squad_id,'term_year'=>$this->term_year,'term_season'=>$this->term_season,'week'=>(int)$this->week])->pluck('memo')->toArray();
            if (count($memos)>0) {
                for ($i=0; $i < count($memos); $i++) { 
                    $memos[$i] = ($i+1)."、".$memos[$i];
                }
                return implode("<br>", $memos);
            }else{
                return "N/A";
            }
        })->style('max-width:400px;word-break:break-all;');
        $grid->footer(function(){
            //dd(Input::get());
            $where['status'] = '1';
            $temp_string = "";
            if (!empty(Input::get('term_year'))) {
                $where['term_year'] = Input::get('term_year');
                $temp_string .= "term_year=".Input::get('term_year')."&";
            }if (!empty(Input::get('term_season'))) {
                $where['term_season'] = Input::get('term_season');
                $temp_string .= "term_season=".Input::get('term_season')."&";
            }
            if (!empty(Input::get('week'))) {
                $where['week'] = Input::get('week');
                $temp_string .= "week=".Input::get('week')."&";
            }

            if (!empty(Input::get('squad_section'))) {
                $where['squad_section'] = Input::get('squad_section');
                $temp_string .= "squad_section=".Input::get('squad_section')."&";
            }

            if (!empty(Input::get('squad_group'))) {
                $where['squad_group'] = Input::get('squad_group');
                $temp_string .= "squad_group=".Input::get('squad_group')."&";
            }

            if (!empty(Input::get('squad_batch'))) {
                $where['squad_batch'] = Input::get('squad_batch');
                $temp_string .= "squad_batch=".Input::get('squad_batch')."&";
            }

            if (!empty(Input::get('squad_type'))) {
                $where['squad_type'] = Input::get('squad_type');
                $temp_string .= "squad_type=".Input::get('squad_type')."&";
            }

            if (!empty(Input::get('squad_no'))) {
                $where['squad_no'] = Input::get('squad_no');
                $temp_string .= "squad_no=".Input::get('squad_no')."&";
            }

            if (!empty(Input::get('class_teacher'))) {
                $where['class_teacher'] = Input::get('class_teacher');
                $temp_string .= "class_teacher=".Input::get('class_teacher')."&";
            }
            $export_string = substr($temp_string, 0,-1);
            $total = Student_lost::where($where)->sum('lost_students');
            
            if (!empty(Input::get())) {
                return "<div><div class='col-xs-4'></div><div class='col-xs-2'><span style='text-align:center;color:red;font-size:25px;'>流失总数 ： $total</span></div><div class='col-xs-2'><a class='btn btn-danger' href='export_lost_detail?".$export_string."'>查看&导出详细流失名单</a></div><div class='col-xs-4'></div></div>";
            }
            
        });
        $grid->disableCreateButton();
        //$grid->disableExport();
        //$grid->disableRowSelector();
        $grid->disableActions();
        $filename="流失统计";
        $grid->exporter(new ExcelExpoter($grid,$filename));
        $grid->expandFilter();
        $grid->filter(function ($filter) {
            $term = Term::where('status','0')->first();
            $filter->column(1/3, function ($filter) use ($term) {
                $filter->disableIdFilter();
                $filter->equal('term_year','学年')->year()->default($term->year);
                $filter->equal('squad_section', trans('admin.section'))->select(Class_section_group::where('pid','=','0')->get()->pluck('name', 'id'));
                // $filter->equal('squad_section', trans('admin.section'))->select(Class_section_group::where('pid','=','0')->get()->pluck('name', 'id'))->load('squad_group', '/api/get_group');
                $filter->equal('squad_type', trans('班型'))->select(Class_type::all()->pluck('name', 'id'));
            });
            $filter->column(1/3, function ($filter) use ($term) {
                $filter->equal('term_season','学期')->select(['1'=>'春季学期','2'=>'秋季学期'])->default($term->season);
                $filter->equal('squad_group',trans('admin.group'))->select(Class_section_group::where('pid','!=','0')->get()->pluck('name', 'id'));
                // $filter->equal('squad_group', trans('admin.group'))->select(function($id){
                //     return Class_section_group::options($id);
                // }); 
                $filter->equal('squad_no', trans('班号'));
            });
            $filter->column(1/3, function ($filter)  use ($term) {
                for ($i=1; $i < 25; $i++) { 
                    $week[$i] = "第 ".$i." 周";
                }
                $current_week = ceil((time()-strtotime($term->start_date))/3600/24/7);
                $filter->equal('week', trans('周'))->select($week)->default("$current_week");
                $filter->equal('squad_batch',trans('admin.batch'))->year_month();
                //获取所有角色为班主任的管理员
                $role_id = DB::table('admin_roles')->where('name','班主任')->pluck('id')->toArray();
                //dd($role_id);
                $Tutor = DB::table('admin_users as au')->join('admin_role_users as aru','au.id','=','aru.user_id')
                    ->where('aru.role_id',$role_id)->pluck('au.en_name','au.id')->toArray();
                //dd($Tutor);
                $filter->equal('class_teacher', trans('admin.class_teacher'))->select($Tutor);
            });
        });
        return $grid;
    }

    public function export_index(Request $request)
    {
        return Admin::content(function (Content $content) use($request) {
            $content->header('学员流失详情');
            $content->description(trans('admin.list'));
            
            $content->body($this->export_lost_detail($request)->render());
        });
    }
    public function export_lost_detail(Request $request){
        $grid = new Grid(new Lost_detail());
        if (!empty(Input::get('term_year'))) {
                $where['term_year'] = Input::get('term_year');
            }if (!empty(Input::get('term_season'))) {
                $where['term_season'] = Input::get('term_season');
            }
            if (!empty(Input::get('week'))) {
                $where['week'] = Input::get('week');
            }

            if (!empty(Input::get('squad_section'))) {
                $where['squad_section'] = Input::get('squad_section');
            }

            if (!empty(Input::get('squad_group'))) {
                $where['squad_group'] = Input::get('squad_group');
            }

            if (!empty(Input::get('squad_batch'))) {
                $where['squad_batch'] = Input::get('squad_batch');
            }

            if (!empty(Input::get('squad_type'))) {
                $where['squad_type'] = Input::get('squad_type');
            }

            if (!empty(Input::get('squad_no'))) {
                $where['squad_no'] = Input::get('squad_no');
            }

            if (!empty(Input::get('class_teacher'))) {
                $where['class_teacher'] = Input::get('class_teacher');
            }
        $grid->model()->where($where);
        $grid->paginate(20);
        $grid->student_id('学生信息')->display(function($student_id){
            $student = Student::find($this->student_id);
            return $student->en_name."【".$student->ch_name."】";
        });

        $grid->squad_id('流失前班级')->display(function($squad_id){
            return Squad::find($this->squad_id)->full_name;
        });

        $grid->apply_type('流失类型')->display(function($apply_type){
            if ($this->apply_type==2) {
                return '休学';
            }
            if ($this->apply_type==3) {
                return '退学';
            }
        })->sortable();
        $grid->column('流失时间')->display(function($term_season){
            if ($this->term_season=='1') {
                return "【".$this->term_year."年】【春季学期】【第".$this->week."周】";
            }elseif ($this->term_season=='2') {
                return "【".$this->term_year."年】【秋季学期】【第".$this->week."周】";
            }
        })->sortable();
        $grid->memo('流失原因')->style('max-width:600px;word-break:break-all;');
        $grid->study_jindu('流失时学习进度')->style('max-width:600px;word-break:break-all;');
        $grid->disableCreateButton();
        //$grid->disableExport();
        $filename="流失详情";
        $grid->exporter(new ExcelExpoter($grid,$filename));
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableFilter();
        
        return $grid;
    }
}
